#include"Stack.h"
#define DEFSTACKSIZE 100
void CheckCapacity(Stack* ps)
{
  if(ps->size >=  ps->_capacity)
  {
    ps->_capacity *= 2;
    ps->_a = (STDataType *)realloc(ps->_a,ps->_capacity*sizeof(STDataType));
  }
}
// 初始化栈     
void StackInit(Stack* ps)
{
  ps->_a = (STDataType *)calloc(DEFSTACKSIZE,sizeof(STDataType));
  ps->_capacity = DEFSTACKSIZE;
  ps->size = 0;
}
// 入栈     
void StackPush(Stack* ps, STDataType data)
{
  CheckCapacity(ps);
  ps->_a[ps->size] = data;
  ps->size++;
}
// 出栈     
void StackPop(Stack* ps)
{
  if(ps->size == 0)
  return ;
  ps->size--;
}
// 获取栈顶元素     
STDataType StackTop(Stack* ps)
{
  if(ps->size == 0)
    return (STDataType)0;

  return ps->_a[ps->size-1];
}
// 获取栈中有效元素个数     
int StackSize(Stack* ps)
{
  return ps->size;
}
// 检测栈是否为空，如果为空返回非零结果，如果不为空返回0     
int StackEmpty(Stack* ps)
{
  return ps->size == 0;
}
// 销毁栈     
void StackDestroy(Stack* ps)
{
  if(ps->_a)
  {
    free(ps->_a);
    ps->_a = NULL;
    ps->size = 0;
    ps->_capacity = 0;
  }
}
